package com.west.imclient.handler.impl;

import imserverentity.entity.IChattable;
import imserverentity.entity.impl.Client;

import java.util.List;
import java.util.Map;

import javax.swing.JTextPane;

import com.west.imclient.handler.IHandler;
import com.west.imclient.view.MeetingRoomView;

public class ShakeHandler implements IHandler{

	private Map<String, Object> params;
	private MeetingRoomView meetingRoomView;
	
	public ShakeHandler(Map<String, Object> params, MeetingRoomView meetingRoomView) {
		this.params = params;
		this.meetingRoomView = meetingRoomView;
	}
	
	public void handle() {
		IChattable from = (IChattable) params.get("from");
		String to = (String) params.get("to");
		JTextPane jtp = meetingRoomView.getJtp();
		jtp.setText(jtp.getText()+ "\n" + ((Client) from).getAccount() + " shake " + to);
		shake();
	}
	
	private void shake() {
		int x = meetingRoomView.getX();
		int y = meetingRoomView.getY();
		for (int i = 0; i < 10; i++) {// 使用for循环让窗体震动20次
			int newXPoint = (int) (x + Math.pow(-1, i) * 10);// 根据循环次数计算新点的位置
			int newYPoint = (int) (y + Math.pow(-1, i) * 10);// 根据循环次数计算新点的位置
			meetingRoomView.setLocation(newXPoint, newYPoint);// 设置窗体的显示位置
			
			try {
				Thread.sleep(50);// 线程休眠0.05秒来实现动态效果
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
